/*
 * Viewer.java
 *
 * Created on March 6, 2007, 1:47 PM
 */

package astrolabe.imgprocessing;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.awt.image.*;
import java.io.*;
import java.util.LinkedList;
import javax.imageio.*;
import javax.swing.*;
/**
 *
 * @author Tobias Knutsson
 */
public class VisorViewer extends JComponent
{
    
    BufferedImage img;
    BufferedImage processed;
    
    Line2D botLine = new Line2D.Double(0,0,0,0);
    double initialRot = 0F;
    LinkedList<Point> path = new LinkedList();
    int traveledPoints = 0;
    
    JFrame f = new JFrame("VisorViewer");
    JTextArea log = new JTextArea(1,7);
    
    public VisorViewer()    {

        f.setPreferredSize(new Dimension(640, 680));
        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        JScrollPane logP = new JScrollPane(log);
        logP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        
        JPanel gl = new JPanel(new BorderLayout());    
        gl.add(this, BorderLayout.NORTH);
        gl.add(logP, BorderLayout.CENTER);
        
        f.add(gl);
        f.pack();
        f.setVisible(true);
    }
    

    public void Log(String s)
    {
        log.append(s+"\n");
        log.setCaretPosition(log.getDocument().getLength());
    }

    public void traveled()
    {   traveledPoints++;   }
    public void setImage(BufferedImage img)
    {   this.img = img; }
    public void setPath(LinkedList<Point> path)
    {   this.path = path;   }
    public void setProcessed(BufferedImage processed)
    {   this.processed = processed;   }
    public void setBotLine(Line2D botLine)
    {   this.botLine = botLine;   }
    public void setInitialRot(double initialRot)
    {   this.initialRot = initialRot;   }
    
    public void rePaint()
    {
        paint(this.getGraphics());
    }
    
    public void paint(Graphics g)
    {
        if(img==null)
        {
            g.setColor(Color.RED);
            g.drawString("Waiting for image..",240, 80);
        }
        else
        {
            g.drawImage(img, 0, 0, 640, 480, null);
            g.setXORMode(Color.BLACK);
            g.drawImage(processed, 0, 0, 640, 480, null);
            g.setPaintMode();
            g.setColor(Color.RED);
            g.drawLine((int)botLine.getX1(), (int)botLine.getY1(), (int)botLine.getX2(), (int)botLine.getY2());
            
            
            int xs[] = new int[path.size()];
            int ys[] = new int[path.size()];
            int i = 0;
            
            for(Point p : path)
            {
                xs[i]=(int)p.getX()*5;
                ys[i]=(int)p.getY()*5;
                i++;
            }
            g.setColor(Color.GREEN);
            g.drawPolygon(xs, ys, path.size());
            g.setColor(Color.BLUE);
            g.drawPolygon(xs, ys, traveledPoints+2);
            if(traveledPoints==path.size()) traveledPoints=0;
        }
    }
    public Dimension getPreferredSize() {
        if (img == null) {
             return new Dimension(640,480);
        } else {
           return new Dimension(img.getWidth(null), img.getHeight(null));
       }
    }
    
}
